// miniprogram/pages/learn/videoDetail/index.js
import {LearnModel} from '../../../model/learn'
import {UserModel} from '../../../model/user'
import { baseUrl } from "../../../http/config"
import {isRegister} from '../../../utils/utils'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl:baseUrl,
    id:0,
    data:{},
    currentTime:0,
    isComplate: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id)
    this.data.id = options.id
    this.loadDetail(this.data.id)
    this.data.id = options.id
  },
  loadDetail(id){
    LearnModel.courseDetail(id).then((res) => {
      if(res && res.code == 200) {
        this.setData({
          data: res.data
        })
      }else {
        console.log(res)
      }
      // console.log(this.data.data)
    }).catch(e => {
      console.log(e)
    })
  },
  dig(){
    isRegister()
    let data = {
      courseId:this.data.id
    }
    UserModel.digCourse(data).then((res) => {
      if(res && res.code == 200) {
        this.setData({
          'data.isDig':this.data.data.isDig ^ 1
        })
        if(this.data.data.isDig == 1){
          this.setData({
            'data.digNum':this.data.data.digNum + 1
          })
        }else {
          this.setData({
            'data.digNum':this.data.data.digNum - 1
          })
        }
      }else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadDetail(this.data.id)
  },
  bindplay(e){
    console.log('bindplay', e)
  },
  bindpause(e){
    console.log('bindpause', e)
  },
  bindended(e){
    console.log('bindended', e)
    this.data.isComplate = 1
  },
  bindtimeupdate(e){
    // console.log('bindtimeupdate', e.detail.currentTime, e.detail.duration)
    this.data.currentTime = e.detail.currentTime
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('onHide')
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('onUnload')
    let data = {
      courseId:this.data.id,
      watchCurrent:this.data.currentTime,
      isComplate:this.data.isComplate
    }
    UserModel.userUpdateWatch(data).then((res) => {
      console.log(res)
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})